<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="tp.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <div class="logo">Mikufans</div>
    </header>
    <main>
        <section class="register">
            <form id="register-form">
                <h2>注册</h2>
                <label for="email">邮箱:</label>
                <input type="email" id="email" required>
                <label for="password">密码:</label>
                <input type="password" id="password" required>
                <label for="code">验证码:</label>
                <input type="text" id="code" required>
                <button type="button" id="send-code">发送验证码</button>
                <button type="submit">注册</button>
            </form>
        </section>
    </main>
    <footer>
        <p>&copy; 2024 Mikufans. 不保留所有权利.</p>
    </footer>
    <script>
        document.getElementById('send-code').addEventListener('click', () => {
            const email = document.getElementById('email').value;

            fetch('/send-code', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ email })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
            });
        });

        document.getElementById('register-form').addEventListener('submit', (event) => {
            event.preventDefault();
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            const code = document.getElementById('code').value;

            fetch('/register', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ email, password, code })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                if (data.message === '注册成功') {
                    window.location.href = '/login.html';
                }
            });
        });
    </script>
</body>
</html>
